<?php
// 用户登录注册
header("content-type:text/html;charset=utf-8");
include_once("conn.php");
//设置默认时区
date_default_timezone_set('PRC');
$now_date = date('Y-m-d');

if ($_POST['action'] == 'login') {
    $id = $_POST['uid'];
    $pwd = $_POST['upassword'];
    $sql = "SELECT * FROM tb_user_info WHERE uid ='" . $id . "'";
    $result = mysqli_query($conn, $sql);
    if ($result) {
        if (mysqli_num_rows($result) > 0) {
            $row = mysqli_fetch_assoc($result);
            if ($row['upwd'] == $pwd) {
                if ($row['ustatus'] == "正常") {
                    session_start();
                    $_SESSION['USERINFOS'] = $row;
                    echo "<script>window.location.href='../../index.php';</script>";
                }else{
                    echo "<script>alert('该用户已被管理员禁用！请联系管理员处理！');history.go(-1);</script>";
                }
            } else {
                echo "<script>alert('密码错误！');history.go(-1);</script>";
            }
        } else {
            echo "<script>alert('用户不存在！');history.go(-1);</script>";
        }
    } else {
        echo "<script>alert('登录失败请重试！');history.go(-1);</script>";
    }
}

if ($_POST['action'] == 'reg') {
    $id = $_POST['uid'];
    $pwd = $_POST['upassword'];
    $phone = $_POST['uphone'];
    $name = $_POST['uname'];
    $sql = "SELECT * FROM tb_user_info WHERE uid ='" . $id . "'";
    $result = mysqli_query($conn, $sql);
    if ($result) {
        if (mysqli_num_rows($result) > 0) {
            echo "<script>alert('用户ID已存在！');history.go(-1);</script>";
        } else {
            $sql = "INSERT INTO tb_user_info(uid,uname,upwd, uphone,uregdate) 
                VALUES('" . $id . "','" . $name . "','" . $pwd . "','" . $phone . "','" . $now_date . "')";
            $result = mysqli_query($conn, $sql);
            if ($result) {
                echo "<script>alert('注册成功！');window.location.href='../login_reg.html';</script>";
            } else {
                echo "<script>alert('注册失败请重试！');history.go(-1);</script>";
            }
        }
    } else {
        echo "<script>alert('注册失败请重试！');history.go(-1);</script>";
    }
}
